package com.tpshion.cloud.common.support;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Collection;

/**
 * 业务断言
 */
public class Assert {

    public static void notEmpty(Collection<?> collection, String code, String msg){
        if (CollectionUtils.isEmpty(collection)) {
            throw new CoreException(code, msg);
        }
    }

    public static void notEmpty(Collection<?> collection, ErrorCode errorCode){
        notEmpty(collection, errorCode.getCode(), errorCode.getMsg());
    }

    public static void isEmpty(Collection<?> collection, ErrorCode errorCode){
        if (!CollectionUtils.isEmpty(collection)) {
            throw new CoreException(errorCode);
        }
    }

    public static void notEmpty(String value, String code, String msg){
        if(StringUtils.isEmpty(value)){
            throw new CoreException(code, msg);
        }
    }

    public static void notEmpty(String value, ErrorCode errorCode){
        notEmpty(value, errorCode.getCode(), errorCode.getMsg());
    }

    public static void notNull(Object object, String code, String msg){
        if(null == object){
            throw new CoreException(code,msg);
        }
    }

    public static void notNull(Object object, ErrorCode errorCode){
        notNull(object, errorCode.getCode(), errorCode.getMsg());
    }

    public static void isNull(Object object, ErrorCode errorCode){
        isNull(object,errorCode.getCode(),errorCode.getMsg());
    }

    public static void isNull(Object object, String code, String msg){
        if(null != object){
            throw new CoreException(code,msg);
        }
    }

    public static void isTrue(boolean expression, ErrorCode errorCode) {
        isTrue(expression,errorCode.getCode(),errorCode.getMsg());
    }

    public static void isTrue(boolean expression, String code, String msg) {
        if (!expression) {
            throw new CoreException(code,msg);
        }
    }
}
